package com.huixuebao.settle.controller;


import cn.hutool.core.bean.BeanUtil;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.bean.RequestData;
import com.huixuebao.common.bean.ResponseData;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.common.util.CommonUtil;
import com.huixuebao.settle.common.config.GlobalConfig;
import com.huixuebao.settle.dto.InsureOrderShortDTO;
import com.huixuebao.settle.dto.InsureOrderDTO;
import com.huixuebao.settle.dto.OrderDTO;
import com.huixuebao.settle.entity.InsuredOrderEntity;
import com.huixuebao.settle.service.InsureOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fatao_yu
 * @version 1.0
 * @date 2020-10-31 22:04
 */
@Slf4j
@RestController
@RequestMapping("/insure/order")
public class InsureOrderController {

    @Autowired
    private InsureOrderService insureOrderService;

    @Autowired
    private GlobalConfig globalConfig;

    /**
     * 提交保单
     * @param requestData
     * @return
     */
    @PostMapping("/submit")
    public ResponseData submitOrder(@RequestBody RequestData requestData) {
        try {
            OrderDTO dto = CommonUtil.resolverRequestData(requestData, OrderDTO.class, globalConfig.getPublicKey(), globalConfig.getDataKey());
            return insureOrderService.submitOrder(dto);
        } catch (Exception e) {
            log.error("数据解析失败：{}", e);
            return new ResponseData(ResultCodeEnum.DATA_PARSE_ERROR);
        }

    }

    /**
     * 更新保单
     */
    @PostMapping("/update")
    public ResponseData updateOrder(@RequestBody  RequestData requestData) {
        try {
            InsureOrderDTO dto = CommonUtil.resolverRequestData(requestData, InsureOrderDTO.class, globalConfig.getPublicKey(), globalConfig.getDataKey());
            insureOrderService.updateOrder(dto);
            //insureOrderService.updateOrder(requestData);
            return new ResponseData(ResultCodeEnum.SUCCESS);
        } catch (Exception e) {
            log.error("数据解析失败：{}", e);
            return new ResponseData(ResultCodeEnum.DATA_PARSE_ERROR);
        }
    }

    /**
     * 获取订单下面投保人的id,查询当前保单信息
     * @param phone
     * @return
     */
    @GetMapping("/list")
    public ApiResult<List<InsureOrderShortDTO>> listInsuredOrder(@RequestParam("phone") String phone) {
        ApiResult apiResult = new ApiResult();
        List<InsuredOrderEntity> list = insureOrderService.listInsuredOrders(phone);
        List<InsureOrderShortDTO> data = new ArrayList<>();
        list.stream().forEach(insuredOrderEntity -> {
            InsureOrderShortDTO dto = new InsureOrderShortDTO();
            BeanUtil.copyProperties(insuredOrderEntity, dto);
            data.add(dto);
        });
        apiResult.setData(data);
        return apiResult;
    }

    @GetMapping("/updateSettleState")
    public ApiResult updateSettleState(@RequestParam("policyId") String policyId) {
        return insureOrderService.updateSettleState(policyId, "1");
    }

    @GetMapping("/updateStateAfterPay")
    public ApiResult updateStateAfterPay(@RequestParam("policyId") String policyId, String settleState) {
        return insureOrderService.updateSettleState(policyId, settleState);
    }
}
